package elitech.vietnam.weatherfashion;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import elitech.vietnam.weatherfashion.backgroundtask.LoginTask;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.ultilities.ValueOption;

public class LoginActivity extends Activity implements OnClickListener {

	public static final int REQ_JOIN_OK = 1;

//	private FileOption fileOption;

	private TextView tvRegister;
	private EditText textbox_username, textbox_password;
	private Button button_login;
	private TextView text_error;
	private CheckBox check_remember;
	public ProgressDialog mProgressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		overridePendingTransition(R.anim.fragment_slidein_left,
				R.anim.fragment_slideout_right);

		tvRegister = (TextView) findViewById(R.id.login_image_register);
		textbox_username = (EditText) findViewById(R.id.edtUsername);
		textbox_username.clearFocus();
		textbox_password = (EditText) findViewById(R.id.edtPassword);
		textbox_password.clearFocus();
		button_login = (Button) findViewById(R.id.btnLogin);
		text_error = (TextView) findViewById(R.id.login_text_error);
		check_remember = (CheckBox) findViewById(R.id.chbAutoLogin);

		button_login.setOnClickListener(this);
		tvRegister.setOnClickListener(this);

		/**************************/
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setTitle(getResources().getString(
				R.string.login_progress_title));
		mProgressDialog.setMessage(getResources().getString(
				R.string.setting_change_password_processing));
		mProgressDialog.setCancelable(false);

		initValue();
	}

	private void initValue() {

		// fileOption = new FileOption(this);
		ValueOption option = GlobalValue.getInstance(this).readOptionInfo();
		textbox_username.setText(option.getUsername());
		textbox_password.setText(option.getPassword());
		// textbox_username.setText(AppConfig.getInstance().valueOption
		// .getUsername());
		// textbox_password.setText(AppConfig.getInstance().valueOption
		// .getPassword());
		check_remember.setChecked(true);
	}

	@Override
	public void onClick(View v) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(textbox_password.getWindowToken(), 0);
		imm.hideSoftInputFromWindow(textbox_username.getWindowToken(), 0);

		switch (v.getId()) {
		case R.id.btnLogin:

			text_error.setText("");
			ValueOption option = GlobalValue.getInstance(this).readOptionInfo();

			// if (check_remember.isChecked()) {
			// AppConfig.getInstance().valueOption.setAutoLogin(true);
			// AppConfig.getInstance().valueOption
			// .setUsername(textbox_username.getText().toString());
			// AppConfig.getInstance().valueOption
			// .setPassword(textbox_password.getText().toString());
			// } else {
			// AppConfig.getInstance().valueOption.setAutoLogin(false);
			// AppConfig.getInstance().valueOption.setUsername("");
			// AppConfig.getInstance().valueOption.setPassword("");
			// }
			if (check_remember.isChecked()) {
				option.setAutoLogin(true);
				option.setUsername(textbox_username.getText().toString());
				option.setPassword(textbox_password.getText().toString());
			} else {
				option.setAutoLogin(false);
				option.setUsername(textbox_username.getText().toString());
				option.setPassword("");
			}
			 GlobalValue.getInstance(this).writeOptionInfo(option);
			// fileOption.WriteOption(option);

			String msg = validate();
			if (msg != null)
				setError(msg);
			else
				new LoginTask(this).execute(textbox_username.getText()
						.toString(), textbox_password.getText().toString());
			break;

		case R.id.login_image_register:
			Intent intent = new Intent(this, RegisterActivity.class);
			startActivityForResult(intent, REQ_JOIN_OK);
			break;

		default:
			break;
		}
	}

	public void setError(String error) {
		text_error.setText(error);
	}

	private String validate() {
		if (textbox_username.getText().toString().equals(""))
			return getString(R.string.please_enter_username);
		if (textbox_password.getText().toString().equals(""))
			return getString(R.string.please_enter_password);
		return null;
	}

	@Override
	public void onBackPressed() {
		setResult(RESULT_CANCELED);
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		// hide keyboard
		try {
			getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		} catch (Exception e) {
		}
		super.onDestroy();
		if (requestCode != REQ_JOIN_OK)
			return;
		if (resultCode == RESULT_OK) {
			textbox_username.setText(data.getStringExtra("username"));
			textbox_password.setText(data.getStringExtra("password"));
			setError("");
		}
	}

}
